module OpenAccount
    
    module  OpenAccountNotify
        
        def welcome(user)
            setup_email(user)

            # Email header info
            @subject += "Welcome to #{AuthenticatedSession::Application_Name}!"

            # Email body substitutions
            @body["name"] = "#{user.fullname}"
            @body["nickname"] = user.nickname
            @body["Application_Name"] = AuthenticatedSession::Application_Name

        end

        def signup_notification(user)
            setup_email(user)
            @subject    += 'Please activate your new account'

            @body[:url]  = "http://YOURSITE/activate/#{user.activation_code}"
        end

        def activation(user)
            setup_email(user)
            @subject    += 'Your account has been activated!'
            @body[:url]  = "http://YOURSITE/"
        end

        protected

        def setup_email(user)
            self.raise_delivery_errors = true
            @recipients = "#{user.email}"
            @from       = AuthenticatedSession::Email_From
            @subject    = "[#{AuthenticatedSession::Application_Name}] "
            @sent_on    = Time.now
            @headers['Content-Type'] = "text/plain; charset=#{AuthenticatedSession::Mail_Charset}; format=flowed"
            @body[:user] = user
        end
        
        
        def signup(user, url=nil)
            
            setup_email(user)
            
            # Email header info
            @subject += "Welcome to #{OpenAccount::OpenAccountConfiguration.config(:app_name)}!"
            
            # Email body substitutions
            @body["name"] = "#{user.firstname} #{user.lastname}"
            @body["login"] = user.login
            @body["url"] = url || OpenAccount::OpenAccountConfiguration.config(:app_url).to_s
            @body["app_name"] = OpenAccount::OpenAccountConfiguration.config(:app_name).to_s
        end
        
        def reset_password(user)
            setup_email(user)
            @subject    += 'Your password has been reset'
        end
        
        def forgot_password(user, url=nil)
            setup_email(user)
            
            # Email header info
            @subject += "Forgotten password notification"
            
            # Email body substitutions
            @body["name"] = "#{user.firstname} #{user.lastname}"
            @body["login"] = user.login
            @body["url"] = url || OpenAccount::OpenAccountConfiguration.config(:app_url).to_s
            @body["app_name"] = OpenAccount::OpenAccountConfiguration.config(:app_name).to_s
        end
        
        def change_password(user, password, url=nil)
            setup_email(user)
            
            # Email header info
            @subject += "Changed password notification"
            
            # Email body substitutions
            @body["name"] = "#{user.firstname} #{user.lastname}"
            @body["login"] = user.login
            @body["password"] = password
            @body["url"] = url || OpenAccount::OpenAccountConfiguration.config(:app_url).to_s
            @body["app_name"] = OpenAccount::OpenAccountConfiguration.config(:app_name).to_s
        end

    end
    
end
